package com.atguigu.eduservice.controller;

import com.alibaba.excel.EasyExcel;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.ExcelData;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.TeachQuery;

import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.Session;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Api(description ="导出讲师Excel")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/edu-teacher")
public class WebExcelExport {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private EduCourseService eduCourseService;

    @GetMapping("/export")
    // 自己做的excel的导出所有讲师列表，没有做条件查询导出，如果要做条件只能拼在url后面，在data里面进双向绑定获取变量
    public void exportEmailZip(HttpServletResponse response, HttpSession session) throws IOException {
        String merchantName = "ZhanK";
        // 时间周期
        // String period = DateTimeUtils.getDateStr(DateTimeUtils.parseIsoDate(startDate)) + "-" + DateTimeUtils.getDateStr(
        // DateTimeUtils.parseIsoDate(endDate));
        String fileName = "Test.xlsx";
        // 这里 需要指定写用哪个class去写
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        //response.setContentType()对应文件类型 application/vnd.ms-excel--MSEXCEL
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        List<EduTeacher> list = eduTeacherService.list(null);

        EasyExcel.write(response.getOutputStream(), EduTeacher.class)
//                    .registerWriteHandler(EasyExcelUtil.getStyleStrategy())
                .sheet("模板")
                .doWrite(list);
    }
    @GetMapping("/export1")
    // 自己做的excel的导出所有讲师列表，没有做条件查询导出，如果要做条件只能拼在url后面，在data里面进双向绑定获取变量   @ModelAttribute
    public void exportEmailZip1(HttpServletResponse response, HttpSession session , @ModelAttribute CoursePublishVo coursePublishVo) throws IOException {
        String merchantName = "ZhanK";
        // 时间周期
        // String period = DateTimeUtils.getDateStr(DateTimeUtils.parseIsoDate(startDate)) + "-" + DateTimeUtils.getDateStr(
        // DateTimeUtils.parseIsoDate(endDate));
        String fileName = "Test.xlsx";
        // 这里 需要指定写用哪个class去写
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        //response.setContentType()对应文件类型 application/vnd.ms-excel--MSEXCEL
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        QueryWrapper<CoursePublishVo> wrapper = new QueryWrapper<>();
        List<CoursePublishVo> list = eduCourseService.getPageCourseExcel(coursePublishVo);

        EasyExcel.write(response.getOutputStream(), CoursePublishVo.class)
//                    .registerWriteHandler(EasyExcelUtil.getStyleStrategy())
                .sheet("课程数据列表")
                .doWrite(list);
    }


    @GetMapping(value = ("/test01"))
    public R test(HttpSession session) {

        Object excelSelect = session.getAttribute("excelSelect");

        System.out.println(session);
        System.out.println("==============" + excelSelect);
        return R.ok().data("ADD", excelSelect);
    }

    //创建方法返回定义的DemoData的数据集合，这个正常是去数据库的表取出来封装到实体类
    public List<ExcelData> data() {
        ArrayList<ExcelData> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ExcelData demoData = new ExcelData();
            demoData.setSno(i);
            demoData.setSname("娜美" + i);
            list.add(demoData);
            System.out.println(demoData);
        }

        return list;
    }


}
